package com.test.apollotest.listence;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @description:
 * @author: zsw
 * @time: 2021/4/15 14:47
 */
@Slf4j
@Component
public class ApolloListen {

    @ApolloConfigChangeListener(value = {"redis-source","application"})
    private void anotherOnChange(ConfigChangeEvent changeEvent) {
        String namespace = changeEvent.getNamespace();
        log.info("apollo namespace:", namespace);
        Set<String> changedKey = changeEvent.changedKeys();
        changedKey.forEach(key -> {
            ConfigChange change = changeEvent.getChange(key);
            log.info(String.format("Found change（apollo变更）： key: %s, oldValue: %s, newValue: %s, changeType: %s ",
                    change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                    change.getChangeType()));
        });

    }
}
